﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace He.Web.manage.olservice
{
    public partial class list : He.Web.UI.Manage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                chkLoginLevel("viewolservice");
                RpgBind();
            }
        }

        #region 数据绑定
        private void RpgBind()
        {
            rptList.DataSource = new He.BLL.olservice().GetList("id>0 order by sort_id asc");
            rptList.DataBind();
        }
        #endregion

        #region 获取分组
        public string GetServiceGroup(int groupId)
        {
            BLL.olservice_group bll = new BLL.olservice_group();
            Model.olservice_group group = bll.GetModel(groupId);
            if (group != null)
            {
                return group.group_name;
            }
            return string.Empty;
        }
        #endregion

        //修改属性
        protected void ibtnRed_Command(object sender, CommandEventArgs e)
        {
            chkLoginLevel("editolservice");
            int id = Convert.ToInt32(e.CommandArgument);
            BLL.olservice bll = new BLL.olservice();
            Model.olservice model = bll.GetModel(id);
            model.is_red = model.is_red == 1 ? 0 : 1;
            bll.Update(model);
            //更新缓存
            RemoveAllCache();
            SaveLogs("[在线客服管理]修改属性");
            Response.Redirect(Request.RawUrl);
        }

        //删除
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            chkLoginLevel("delolservice");
            BLL.olservice bll = new BLL.olservice();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((HiddenField)rptList.Items[i].FindControl("HiddenId")).Value);
                if (cb.Checked)
                {
                    He.Model.olservice model = bll.GetModel(id);
                    bll.Delete(id);
                }
            }
            SaveLogs("[在线客服管理]批量删除");
            ShowSuccess("批量删除成功啦！", Request.RawUrl);
        }

        //保存排序
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            chkLoginLevel("editolservice");
            BLL.olservice bll = new BLL.olservice();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            SaveLogs("[在线客服管理]批量保存排序");
            ShowSuccess("排序保存成功啦！", Request.RawUrl);
        }
    }
}
